package com.ca.ui.core.linstener;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ca.ui.core.util.ImageUtils;
import com.youth.banner.listener.OnPageChangeListener;

public class BannerCardOnPageChangeListener implements OnPageChangeListener {

    private final Context context;
    private final ImageView imageView;
    private final String[] imageUrlArray;

    public BannerCardOnPageChangeListener(Context context, ImageView imageView, String[] imageUrlArray) {
        this.context = context;
        this.imageView = imageView;
        this.imageUrlArray = imageUrlArray == null ? new String[0] : imageUrlArray;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        ImageUtils.loadFuzzyImage(context, imageUrlArray[position], imageView, 8);
        imageView.post(() -> {
            int width = imageView.getWidth();
            ViewGroup.LayoutParams params = imageView.getLayoutParams();
            params.height = (int) (width * 2.0 / 3);
            imageView.setLayoutParams(params);
        });
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
